package com.rcw.filelib.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;

import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;

import com.rcw.filelib.DocScannerTask;
import com.rcw.filelib.FilePicker;
import com.rcw.filelib.FileType;
import com.rcw.filelib.PickerManager;
import com.rcw.filelib.R;
import com.rcw.filelib.Utils;
import com.rcw.filelib.adapter.SectionsPagerAdapter;
import com.rcw.filelib.bean.Document;
import com.rcw.filelib.callback.FileResultCallback;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.Predicate;


public class FilePickerActivity extends AppCompatActivity implements DocFragment.OnFragmentInteractionListener {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_pick_main);
        initPickManager();
        initView();
        setUpViewPager();
        getFile();
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    private void setUpViewPager() {
        SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());

        ArrayList<FileType> supportedTypes = PickerManager.getInstance().getFileTypes();
        for (int index = 0; index < supportedTypes.size(); index++) {
            adapter.addFragment(DocFragment.newInstance(supportedTypes.get(index)), supportedTypes.get(index).title);
        }

        viewPager.setOffscreenPageLimit(supportedTypes.size());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initPickManager() {
//        String[] zips = {".zip", ".rar"};
//        String[] xmls = {".xml"};
        PickerManager pickerManager = PickerManager.getInstance();
        ArrayList<FileType> list = pickerManager.getFileTypes();
        if (list.size()==0) {
            pickerManager.addDocTypes();
//            pickerManager.addFileType(new FileType("ZIP", zips, 0));
//            pickerManager.addFileType(new FileType("XML", xmls, 0));
        }
        pickerManager.setDocSupport(true);
    }

    public void getFile() {
        new DocScannerTask(this, new FileResultCallback<Document>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onResultCallback(List<Document> files) {
//                Log.d("List<Document>  :::::::", files.toString());
                progressBar.setVisibility(View.GONE);
                setDataOnFragments(files);
            }
        }).execute();

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void setDataOnFragments(List<Document> files) {
        SectionsPagerAdapter sectionsPagerAdapter = (SectionsPagerAdapter) viewPager.getAdapter();
        if (sectionsPagerAdapter != null) {
            for (int index = 0; index < sectionsPagerAdapter.getCount(); index++) {
                DocFragment docFragment = (DocFragment) getSupportFragmentManager()
                        .findFragmentByTag(
                                "android:switcher:" + R.id.viewPager + ":" + index);
                if (docFragment != null) {
                    FileType fileType = docFragment.getFileType();
                    if (fileType != null)
                        docFragment.updateList(filterDocuments(fileType.extensions, files));
                }
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private ArrayList<Document> filterDocuments(final String[] type, List<Document> documents) {
        final Predicate<Document> docType = new Predicate<Document>() {
            @Override
            public boolean test(Document document) {
                return document.isThisType(type);
            }

            public boolean apply(Document document) {
                return document.isThisType(type);
            }
        };

        return new ArrayList<>(Utils.filter(new HashSet<>(documents), docType));
    }


    @Override
    public void onFragmentInteraction(Document doc) {
        setResult(FilePicker.FILE_PICKER_RESULT_CODE, new Intent().putExtra(FilePicker.FILE_PICKER_INTENT_PARAM_DOCUMENT, doc));
//        Log.e("", doc.getPath());
        finish();
    }
}
